package com.kye.express.business.web;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.databinding.ActivityKyewebBinding;
import com.kye.express.view.ToastManager;
import com.kye.webview.FragmentKeyDown;
import com.kye.webview.WebViewFragment;

/**
 * 公共web 页面
 */
public class KyeWebActivity extends BaseToolbarActivity {
    private static final String TAG = "MainActivity";
    private static final String ARG_URL = "url";
    private static final String ARG_TITLE = "title";
    private static final String ARG_CAN_SHARE = "can_share";

    private WebViewFragment mWebViewFragment;
    private ActivityKyewebBinding mBinding;
    private Context mContext;
    private boolean mCanShare = false;//是否需要分享

    public static void startActivity(Context context,String url,String title,boolean canShare) {
        Intent intent = new Intent(context, KyeWebActivity.class);
        intent.putExtra(ARG_URL,url);
        intent.putExtra(ARG_TITLE,title);
        intent.putExtra(ARG_CAN_SHARE,canShare);
        context.startActivity(intent);
    }

    public static void startActivityForNewTask(Context context,String url,String title,boolean canShare) {
        Intent intent = new Intent(context, KyeWebActivity.class);
        intent.putExtra(ARG_URL,url);
        intent.putExtra(ARG_TITLE,title);
        intent.putExtra(ARG_CAN_SHARE,canShare);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_kyeweb);
        mContext = this;
        setUpToolbar((Toolbar) findViewById(R.id.toolbar));
        String url = getIntent().getStringExtra(ARG_URL);
        String title = getIntent().getStringExtra(ARG_TITLE);
        mCanShare = getIntent().getBooleanExtra(ARG_CAN_SHARE,false);
        if (!TextUtils.isEmpty(url)) {
            initWebFragment(url);
        }
        if (!TextUtils.isEmpty(title)) {
            setTitle(title);
        }
    }

    private void initWebFragment(String url) {
        mWebViewFragment = WebViewFragment.newInstance(url, true);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fgcontainer, mWebViewFragment).commit();
        //之所以这么做，是因为webview初始化是需要一些时间，等webview初始化ok后才能返回回来
        mWebViewFragment.setWebViewCallback(new WebViewFragment.WebViewCallback() {
            @Override
            public void callBack(WebView webView) {
                if (webView != null) {
                    //TODO 可以进行js和native的交互处理
                }
            }
        });
    }

    //处理fragment(网页)返回
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mWebViewFragment != null) {
            FragmentKeyDown mFragmentKeyDown = mWebViewFragment;
            if (mFragmentKeyDown.onFragmentKeyDown(keyCode, event)) {
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }

        return super.onKeyDown(keyCode, event);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (mCanShare){
            getMenuInflater().inflate(R.menu.menu_share,menu);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.menu_share:
                ToastManager.show(mContext,"分享",Toast.LENGTH_SHORT);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
